package jscinttools.scint;
import java.io.RandomAccessFile;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.lang.String;
import java.lang.Double;
import java.lang.NumberFormatException;
/**
 *  Read a setup file and find its parameter values
 *
 *@author     Marc-Antoine Verdier
 *@created    3 août 2011
 */
public class SetupFileReader
{
	private RandomAccessFile raf;
	private boolean found = false;


	/**
	 *  Constructor for the SetupFileReader object
	 *
	 *@param  setupFullFileName          The full name of the setup file
	 *@exception  FileNotFoundException  Description of the Exception
	 */
	public SetupFileReader(String setupFullFileName) throws FileNotFoundException
	{
		this.raf = new RandomAccessFile(setupFullFileName, "r");
	}


	/**
	 *  Constructor for the SetupFileReader object
	 *
	 *@param  path                       The path of the setup file
	 *@param  setupFile                  The name of the setup file
	 *@exception  FileNotFoundException  Description of the Exception
	 */
	public SetupFileReader(String path, String setupFile) throws FileNotFoundException
	{
		this(path + File.separator + setupFile);
	}


	/**
	 *  Seek the values for a given parameter name that does not start with "#" and
	 *  spaced with "\t"
	 *
	 *@param  paramName        The name of the parameter
	 *@return                  The parameter values
	 *@exception  IOException  Description of the Exception
	 */
	public double[] findParamValues(String paramName) throws IOException
	{
		raf.seek(0);
		double[] paramVal = null;
		String line = new String();
		this.found = false;
		try
		{
			while (((line = raf.readLine()) != null) && (!found))
			{
				if ((line.indexOf(paramName + ":")) >= 0)
					paramVal = splitParamName(line);
			}
		} catch (NumberFormatException nfe)
		{
			System.out.println("Problem reading parameter: " + nfe);
		}
		return paramVal;
	}


	private double[] splitParamName(String line)
	{
		double[] paramVal;
		String[] parts = line.split("\t");
		paramVal = new double[parts.length - 1];
		for (int i = 0; i < parts.length - 1; i++)
		{
			paramVal[i] = Double.valueOf(parts[i + 1]);
			//System.out.println(paramVal[i]);
		}
		found = true;
		return paramVal;
	}
}

